﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WF.Web.Attachment
{
    public partial class ShowThumbImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ImageID = Request.QueryString.Get("ID");

            if (ImageID != null && ImageID.Trim() != "")
            {
                try
                {
                    //建立查询字符串

                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
                    conn.Open();
                    string strSql = "";
                    strSql = "select FileName,ImageThumbData from dbo.Attachment where ID=@ImageID";
                    SqlCommand sqlCmd = new SqlCommand(strSql, conn);
                    sqlCmd.Parameters.AddWithValue("@ImageID", ImageID);
                    SqlDataReader sqlReader = null;
                    try
                    {
                        sqlReader = sqlCmd.ExecuteReader();
                        if (sqlReader.Read())
                        {
                            Response.Clear();
                            Response.ClearContent();
                            Response.ClearHeaders();

                            string strContentType = GetMimeType((string)sqlReader["FileName"].ToString());

                            Response.AppendHeader("content-type", strContentType);
                            Response.ContentType = strContentType; //设置输出类型    

                            Response.BinaryWrite((byte[])sqlReader["ImageThumbData"]);

                            Response.End();
                        }
                        sqlReader.Close();

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                catch (Exception ex)
                {
                    //Response.Write(ex.Message);
                    //this.ShowMessage(ex);
                }
            }
        }

        /// <summary>
        /// 根据文件后缀来获取MIME类型字符串

        /// </summary>
        /// <param name="extension">文件后缀</param>
        /// <returns></returns>
        private static string GetMimeType(string extension)
        {
            string mime = string.Empty;
            extension = extension.ToLower();
            switch (extension)
            {
                case ".avi": mime = "video/x-msvideo"; break;
                case ".bin":
                case ".exe":
                case ".msi":
                case ".dll":
                case ".class": mime = "application/octet-stream"; break;
                case ".csv": mime = "text/comma-separated-values"; break;
                case ".html":
                case ".htm":
                case ".shtml": mime = "text/html"; break;
                case ".css": mime = "text/css"; break;
                case ".js": mime = "text/javascript"; break;
                case ".doc":
                case ".dot":
                case ".docx": mime = "application/msword"; break;
                case ".xla":
                case ".xls":
                case ".xlsx": mime = "application/msexcel"; break;
                case ".ppt":
                case ".pptx": mime = "application/mspowerpoint"; break;
                case ".gz": mime = "application/gzip"; break;
                case ".gif": mime = "image/gif"; break;
                case ".bmp": mime = "image/bmp"; break;
                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".png": mime = "image/jpeg"; break;
                case ".mpeg":
                case ".mpg":
                case ".mpe":
                case ".wmv": mime = "video/mpeg"; break;
                case ".mp3":
                case ".wma": mime = "audio/mpeg"; break;
                case ".pdf": mime = "application/pdf"; break;
                case ".rar": mime = "application/octet-stream"; break;
                case ".txt": mime = "text/plain"; break;
                case ".7z":
                case ".z": mime = "application/x-compress"; break;
                case ".zip": mime = "application/x-zip-compressed"; break;
                default:
                    mime = "application/octet-stream";
                    break;
            }
            return mime;
        }

    }
}